//
//  YHYSearchAddressViewController.m
//  LANGirl
//
//  Created by 杨泓晔 on 16/3/30.
//
//

#import "YHYSearchAddressViewController.h"
#import "YHYSearchCollectionViewCell.h"
#import "YHYSHeadCollectionReusableView.h"
#import "YHYSearchAddressViewController.h"
@interface YHYSearchAddressViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,retain)UIView *myheaderView;
@property (nonatomic,retain)WSCustomButton *goBack;
@property (nonatomic,retain)UICollectionView *myCollectionView;
@property (nonatomic,retain)NSMutableArray *myArrAddress;
@property (nonatomic,retain)NSMutableDictionary *myDic;
@property (nonatomic,retain)NSMutableArray *allKeysArr;

@end


@implementation YHYSearchAddressViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self HeadView];
    [self creatView];
    _myArrAddress = [NSMutableArray array];
    // _myDic = [NSMutableDictionary dictionary];
    
    [self sharesingleton];
}


- (void)HeadView{
    
    
    self.myheaderView = [[UIView alloc]init];
    self.myheaderView.backgroundColor = GH;
    self.myheaderView.userInteractionEnabled = YES;
    //    UITapGestureRecognizer * tapAction = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectAction:)];
    //    [self.myheaderView addGestureRecognizer:tapAction];
    [self.view addSubview:self.myheaderView];
    [self.myheaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.top.mas_equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(self.view.frame.size.width, 139.00*AUTO_WIDTH/2));
        
    }];
    
    
    //返回 左边按钮
    self.goBack = [[WSCustomButton alloc]init];
    [self.goBack setImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    [self.goBack addTarget:self action:@selector(Back:) forControlEvents:UIControlEventTouchUpInside];
    [self.myheaderView addSubview:self.goBack];
    [_goBack mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.myheaderView).offset(51.00*AUTO_WIDTH/2);     //  相对于self.view 顶部 10
        make.left.equalTo(self.myheaderView).offset(20.00*AUTO_WIDTH/2);    //  相对于self.view 左边 10
        make.size.mas_equalTo(CGSizeMake(30.00*AUTO_WIDTH, 30.00*AUTO_WIDTH));
        
    }];
    
    
}

- (void)creatView{
    //flowLayout 控制 UICollectionView布局
    UICollectionViewFlowLayout *flowLatout = [[UICollectionViewFlowLayout alloc]init];
    
    //section内置大小
    flowLatout.sectionInset = UIEdgeInsetsMake(0, 15 * AUTO_HEIGHT, 20 * AUTO_WIDTH, 15 * AUTO_HEIGHT);
    
    // 列间距  默认最小为10,如果比10小 需要重新赋值
    flowLatout.minimumInteritemSpacing = 3 * AUTO_WIDTH;
    
    
    //item 大小 如果只有一种size 不适用代理方法返回大小
    //预估先算 代码优化 提高效率 多种item 大小 使用estimatedItemSize 提高代码运算效率 提高流畅度
    
    
    flowLatout.estimatedItemSize = CGSizeMake((SCREEN_WIDTH - 30 * AUTO_WIDTH - 20 *AUTO_WIDTH)/3, 30 * AUTO_HEIGHT);
    flowLatout.estimatedItemSize = CGSizeMake((SCREEN_WIDTH - 30 * AUTO_WIDTH - 20 *AUTO_WIDTH)/3, 20 * AUTO_WIDTH);
    
    
    
    //行间距 同上
    flowLatout.minimumLineSpacing = 10 * AUTO_HEIGHT;
    
    //item 大小
    // flowLatout.itemSize = CGSizeMake((SCREEN_WIDTH - 30)/3, 200);
    //    flowLatout.sectionHeadersPinToVisibleBounds = YES;
    
    //设置header大小
    flowLatout.headerReferenceSize  = CGSizeMake(SCREEN_WIDTH, 40 * AUTO_WIDTH);
    //    flowLatout.footerReferenceSize = CGSizeMake(SCREEN_WIDTH, 0.5);
    
    
    _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 139/2 * AUTO_WIDTH, self.view.frame.size.width, self.view.frame.size.height - 139 * AUTO_WIDTH/2) collectionViewLayout:flowLatout];
    _myCollectionView.backgroundColor = LV;
    [self.view addSubview:_myCollectionView];
    _myCollectionView.dataSource = self;
    _myCollectionView.delegate = self;
    
    [_myCollectionView registerClass:[YHYSearchCollectionViewCell class] forCellWithReuseIdentifier:@"YHYSearchCollectionViewCell"];
    [_myCollectionView registerClass:[YHYSHeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"CollectionReusableView"];
    [_myCollectionView reloadData];
    
    
    
}


- (void)sharesingleton{
    
    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"citydict" ofType:@"plist"];
    NSMutableDictionary *bigDic = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
    //空大字典
    
    
    self.myDic = [NSMutableDictionary dictionary];
    for (NSString *key in bigDic) {
        NSMutableArray *arr = [bigDic objectForKey:key];
        [self.myDic setObject:arr forKey:key];
    }
    self.allKeysArr = [NSMutableArray arrayWithArray:self.myDic.allKeys];
    
    
    [self.allKeysArr sortUsingSelector:@selector(compare:)];
    
}


- (NSString *)firstCharactor:(NSString *)aString

{
    //转成了可变字符串
    NSMutableString *str = [NSMutableString stringWithString:aString];
    //先转换为带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformMandarinLatin,NO);
    //再转换为不带声调的拼音
    CFStringTransform((CFMutableStringRef)str,NULL, kCFStringTransformStripDiacritics,NO);
    //转化为大写拼音
    NSString *pinYin = [str capitalizedString];
    //获取并返回首字母
    return [pinYin substringToIndex:1];
}


//每个区多少个item
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSString  *key = [self.allKeysArr objectAtIndex:section];
    NSMutableArray *arr = [self.myDic objectForKey:key];
    
    return arr.count;
    
}
//-(NSArray *)sectionIndexTitlesForDSCollectionView:(DSCollectionView *)tableView{
//
//    return _allKeysArr;
//}


// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *str = @"YHYSearchCollectionViewCell";
    YHYSearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    NSString *key = [self.allKeysArr objectAtIndex:indexPath.section];
    NSMutableArray *arr = [self.myDic objectForKey:key];
    NSString *name = [arr objectAtIndex:indexPath.row];
    cell.textL.text = [NSString stringWithString:name];
    
    return cell;
    
    
}


//增补视图
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    
    YHYSHeadCollectionReusableView *reusableView  = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"CollectionReusableView" forIndexPath:indexPath];
    reusableView.backgroundColor = LV;
    
    reusableView.label.text = [_allKeysArr objectAtIndex:indexPath.section];;
    
    return reusableView;
}


//区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _allKeysArr.count;
    
}





- (void)Back:(UIButton *)back{
    NSLog(@"wedqw");
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
